package com.lineage.server.clientpackets;

import com.eric.gui.J_Main;
import com.lineage.config.Config;
import com.lineage.config.ConfigAlt;
import com.lineage.config.ConfigOther;
import com.lineage.config.ConfigRecord;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ChatGlobal;
import com.lineage.server.serverpackets.S_ChatTransaction;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.server.world.World;
import java.util.Calendar;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_ChatGlobal extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_ChatGlobal.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (decrypt.length > 108) {
        _log.warn("人物:" + pc.getName() + "对话(广播)长度超过限制:" + client.getIp().toString());
        client.set_error(client.get_error() + 1);
        return;
      } 
      boolean isStop = false;
      boolean errMessage = false;
      if (pc.hasSkillEffect(64) && !pc.isGm())
        isStop = true; 
      if (pc.hasSkillEffect(161) && !pc.isGm())
        isStop = true; 
      if (pc.hasSkillEffect(1007) && !pc.isGm())
        isStop = true; 
      if (pc.hasSkillEffect(4002)) {
        isStop = true;
        errMessage = true;
      } 
      if (isStop) {
        if (errMessage)
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(242)); 
        return;
      } 
      if (!pc.isGm()) {
        if (pc.getLevel() < ConfigAlt.GLOBAL_CHAT_LEVEL) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(195, String.valueOf(ConfigAlt.GLOBAL_CHAT_LEVEL)));
          return;
        } 
        if (!World.get().isWorldChatElabled()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(510));
          return;
        } 
      } 
      if (pc.hasSkillEffect(9990)) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前受到监禁状态中无法正常说话。"));
        return;
      } 
      if (ConfigOther.SET_GLOBAL_TIME > 0 && !pc.isGm()) {
        Calendar cal = Calendar.getInstance();
        long time = cal.getTimeInMillis() / 1000L;
        if (time - pc.get_global_time() < ConfigOther.SET_GLOBAL_TIME)
          return; 
        pc.set_global_time(time);
      } 
      int chatType = readC();
      String chatText = readS();
//      if (isCommandChat(pc, chatText).booleanValue())
//        return; 
      switch (chatType) {
        case 3:
          chatType_3(pc, chatText);
          if (Config.GUI)
            J_Main.getInstance().addWorldChat(pc.getName(), chatText); 
          break;
        case 12:
          chatType_12(pc, chatText);
          if (Config.GUI)
            J_Main.getInstance().addWorldChat(pc.getName(), chatText); 
          break;
      } 
      if (!pc.isGm())
        pc.checkChatInterval(); 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  private void chatType_12(L1PcInstance pc, String chatText) {
    S_ChatTransaction packet = new S_ChatTransaction(pc, chatText);
    String name = pc.getName();
    Iterator<L1PcInstance> iterator = World.get().getAllPlayers().iterator();
    while (iterator.hasNext()) {
      L1PcInstance listner = iterator.next();
      if (listner.getExcludingList().contains(name))
        continue; 
      if (!listner.isShowTradeChat())
        continue; 
      if (listner.isBadInEnemyList(pc.getName()))
        continue; 
      listner.sendPackets((ServerBasePacket)packet);
    } 
    if (ConfigRecord.LOGGING_CHAT_BUSINESS)
      RecordTable.get().recordeTalk("买卖", pc.getName(), pc.getClanname(), null, chatText); 
  }
  
  private void chatType_3(L1PcInstance pc, String chatText) {
    S_ChatGlobal packet = new S_ChatGlobal(pc, chatText);
    if (pc.isGm()) {
      World.get().broadcastPacketToAll((ServerBasePacket)packet);
      return;
    } 
    String name = pc.getName();
    if (!pc.isGm()) {
      L1ItemInstance item;
      L1Item itemtmp;
      switch (ConfigOther.SET_GLOBAL) {
        case 0:
          if (pc.get_food() >= 6) {
            pc.set_food(pc.get_food() - ConfigOther.SET_GLOBAL_COUNT);
            pc.sendPackets((ServerBasePacket)new S_PacketBox(11, pc.get_food()));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(462));
          return;
        default:
          item = pc.getInventory().checkItemX(ConfigOther.SET_GLOBAL, 
              ConfigOther.SET_GLOBAL_COUNT);
          if (item != null) {
            pc.getInventory().removeItem(item, ConfigOther.SET_GLOBAL_COUNT);
            break;
          } 
          itemtmp = ItemTable.get().getTemplate(ConfigOther.SET_GLOBAL);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, itemtmp.getNameId()));
          return;
      } 
    } 
    Iterator<L1PcInstance> iterator = World.get().getAllPlayers().iterator();
    while (iterator.hasNext()) {
      L1PcInstance listner = iterator.next();
      if (listner.getExcludingList().contains(name))
        continue; 
      if (listner.isBadInEnemyList(pc.getName()))
        continue; 
      if (!listner.isShowWorldChat())
        continue; 
      listner.sendPackets((ServerBasePacket)packet);
    } 
    if (ConfigRecord.LOGGING_CHAT_WORLD)
      RecordTable.get().recordeTalk("公频", pc.getName(), pc.getClanname(), null, chatText); 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
